/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */
package com.ykht.proj.coupon.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ykht.proj.common.core.constant.CommonConstants;
import com.ykht.proj.common.core.exception.BusinessException;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.MerchantTypeEnum;
import com.ykht.proj.coupon.entity.FxBrandMoneyFlow;
import com.ykht.proj.coupon.entity.FxMerchantAccount;
import com.ykht.proj.coupon.mapper.FxMerchantAccountMapper;
import com.ykht.proj.coupon.service.FxMerchantAccountService;
import com.ykht.proj.coupon.vo.FxMerchantAccountBrandVo;
import com.ykht.proj.coupon.vo.FxMerchantAccountVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;

/**
 * b端账户表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:39
 */
@Service
@RequiredArgsConstructor
@Slf4j
@Transactional
public class FxMerchantAccountServiceImpl extends ServiceImpl<FxMerchantAccountMapper, FxMerchantAccount> implements FxMerchantAccountService {

	private final FxMerchantAccountMapper fxMerchantAccountMapper;


	/**
	 * B端账号金额加减
	 * @param type				1公司2品牌3门店
	 * @param tatId				关联id
	 * @param balance			可用余额
	 * @param balanceTotal		总余额=可用+冻结
	 * @param balanceFrozen		冻结金额
	 * @param withdrawal		自己提现金额
	 * @param consumeMoney		累计消费金额
	 * @param chargeTotal		累计充值金额
	 * @param updateUser    修改人
	 */
	@Override
	public void updateMerchantAccount(Integer type, Long tatId, BigDecimal balance, BigDecimal balanceTotal, BigDecimal balanceFrozen, BigDecimal withdrawal, BigDecimal consumeMoney, BigDecimal chargeTotal, Long updateUser) {
		fxMerchantAccountMapper.updateMerchantAccount(type, tatId, balance, balanceTotal, balanceFrozen, withdrawal, consumeMoney, chargeTotal, updateUser);
	}

	/**
	 * 分页查询 b端账户表
	 *
	 * @param page                分页对象
	 * @param fxMerchantAccountVo b端账户表
	 * @return
	 */
	@Override
	public IPage<FxMerchantAccountVo> getFxMerchantAccountPage(Page page, FxMerchantAccountVo fxMerchantAccountVo) {
		return fxMerchantAccountMapper.getFxMerchantAccountPage(page, fxMerchantAccountVo);
	}

	/**
	 * 通过id查询b端账户表详情
	 * @param fxMerchantAccountVo b端账户表
	 * @return R
	 */
	@Override
	public FxMerchantAccountVo getByIdAccountDetails(FxMerchantAccountVo fxMerchantAccountVo) {
		return fxMerchantAccountMapper.getByIdAccountDetails(fxMerchantAccountVo);
	}

	@Override
	public FxMerchantAccount cheeckOrCreateAccont(Integer merchantType,Long tatId){
		if (tatId==null) throw new BusinessException("商家id为空");
		FxMerchantAccount account = this.getOne(Wrappers.lambdaQuery(FxMerchantAccount.class).eq(FxMerchantAccount::getTatId,tatId).eq(FxMerchantAccount::getType,merchantType));
		if (account != null) return account;
		account = new FxMerchantAccount();
		account.setType(merchantType);
		if (merchantType == MerchantTypeEnum.company.getValue()){
			account.setAccount(String.format(CommonConstants.ACCONCE_COMPANY_FORMAT, tatId));
		} else if (merchantType == MerchantTypeEnum.brand.getValue()){
			account.setAccount(String.format(CommonConstants.ACCONCE_BRAND_FORMAT, tatId));
		} else if (merchantType == MerchantTypeEnum.shop.getValue()){
			account.setAccount(String.format(CommonConstants.ACCONCE_SHOP_FORMAT, tatId));
		}
		account.setTatId(tatId);
		account.setBalance(BigDecimal.ZERO);
		account.setBalanceTotal(BigDecimal.ZERO);
		account.setBalanceFrozen(BigDecimal.ZERO);
		account.setWithdrawal(BigDecimal.ZERO);
		account.setConsumeMoney(BigDecimal.ZERO);
		account.setChargeTotal(BigDecimal.ZERO);
		account.setCreateUser(SecurityUtils.getUserId());
		account.setCreateTime(LocalDateTime.now());
		this.save(account);
		return account;
	}

	@Override
	public List<FxMerchantAccountVo> getExport() {
		return fxMerchantAccountMapper.getExport();
	}


}
